
package com.bear.cmcc.NetTestHTTP;

import com.baidu.location.LocationClient;
import com.bear.cmcc.NetTestHTTP.component.CountrySortBean;
import com.bear.cmcc.NetTestHTTP.component.InfoSubBean;
import com.bear.cmcc.NetTestHTTP.component.MyLocationListenner;
import com.bear.cmcc.NetTestHTTP.component.ServersConfigBean;
import com.bear.cmcc.NetTestHTTP.component.json.respond.CountryCityFtpInfoRankRespond;
import com.bear.cmcc.NetTestHTTP.component.json.respond.GetNetDataReturnInfoRespond;
import com.bear.cmcc.NetTestHTTP.util.InforSubUtil;
import com.bear.cmcc.NetTestHTTP.util.LocationUtil;
import com.bear.cmcc.NetTestHTTP.util.NetTestUtil;
import com.bear.cmcc.NetTestHTTP.util.SetUtil;

import android.app.Application;
import android.content.Context;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 全局的频道加载保存
 * 
 * @author jqa
 */
public class NetworkTestApp extends Application {
    private static NetworkTestApp mApp;

    public static boolean isBackRun_celldb = false;// 插入小区数据库是否后台运行

    public static boolean isStop_celldb = false;// 是否停止插入小区数据库

    private static List<InfoSubBean> channels = null;

    private String wantDeleteCityUnil = "";// 市,所有城市名称都过滤掉这个字

    private String beijing = "";// 北京，对北京不定位。

    /**
     * 当前用户点击的省，没有默认用定位的省份，北京不用定位。
     */
    private String currentUserClickProvice = null;

    /**
     * 当前用户点击的城市，没有默认用定位的城市，北京不用定位。
     */
    private String currentUserClickCity = null;
    
    
    //缓存全国数据，不必每次都去查询。
    private ArrayList<CountrySortBean> countrys;
    
    private GetNetDataReturnInfoRespond countryInfo;
    //top排名缓存
    private CountryCityFtpInfoRankRespond topTDCity;
    
//    private ArrayList<CityBean> bottomTDCity;
    
    private CountryCityFtpInfoRankRespond top2GCity;
    
//    private ArrayList<CityBean> bottom2GCity;
    
    private ArrayList<ServersConfigBean> serviceConfig;
    
    /**是否已经登录**/
    private boolean haveLogin=false;
    
    @Override
    public void onCreate() {
        super.onCreate();
        mApp = this;
        channels = new ArrayList<InfoSubBean>();

        mLocationClient = new LocationClient(this);
        myListener = new MyLocationListenner(this);
        mLocationClient.registerLocationListener(myListener);
        LocationUtil.getCurrentCity(this);

        // NetTestUtil.startNetTestService(getBaseContext(), 0);// 启动服务，获取信号强度
        if (SetUtil.getAutoTest(getBaseContext())) {
            NetTestUtil.startAutoNetTestService(getBaseContext());
        }
        wantDeleteCityUnil = getString(R.string.city_name);
        beijing = getString(R.string.beijin);
        
    }

    public void onTerminate() {
        try {
            if (mLocationClient != null) {
                mLocationClient.stop();
                mLocationClient.unRegisterLocationListener(myListener);
            }
            NetTestUtil.stopNetTestService(getBaseContext(), 0);
            
            if(countrys!=null)
            {
                countrys.clear();
                countrys=null;
            }
            countryInfo=null;
            
            if(serviceConfig!=null)
            {
                serviceConfig.clear();
                serviceConfig=null;
            }
            
//            if(bottom2GCity!=null)
//            {
//                bottom2GCity.clear();
//                bottom2GCity=null;
//            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        super.onTerminate();
    }

    public static NetworkTestApp getApp() {
        if (mApp == null)
            throw new NullPointerException("app not create should be terminated!");
        else
            return mApp;
    }

    /**
     * 加载频道
     * 
     * @param context
     * @param isAll
     */
    public static void loadChannelList(Context context, boolean isAll) {// 获得已经存在的频道列表
        String xmlFile = mApp.getApplicationInfo().dataDir + "/files/channels.xml";
        File file = new File(xmlFile);
        try {
            FileOutputStream outStream = null;
            if (file.exists()) {
                FileInputStream inStream = context.openFileInput("channels.xml");
                channels = InforSubUtil.getChannels(inStream, isAll);
            } else {
                InputStream inStream = context.getClass().getClassLoader()
                        .getResourceAsStream("assets/default_channels.xml");
                channels = InforSubUtil.getChannels(inStream, isAll);
                outStream = context.openFileOutput("channels.xml", Context.MODE_PRIVATE);
                InforSubUtil.save(channels, outStream);
                outStream.close();
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    /**
     * 修改
     * 
     * @param context
     * @param newChannels
     */
    public static void modifyChannelList(Context context, List<InfoSubBean> newChannels) {
        FileOutputStream outStream = null;
        try {
            outStream = context.openFileOutput("channels.xml", MODE_PRIVATE);
            InforSubUtil.save(newChannels, outStream);
            outStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (Throwable e) {
            e.printStackTrace();
        }

    }

    public static List<InfoSubBean> getChannelList() {
        return channels;
    }

    public static void setChannelList(List<InfoSubBean> chs) {
        channels = chs;
    }

    /******************************************************** 百度拷贝过来的代码，主要用来百度定位 *******************************************************/
    /**
     * 手机所在的省份。
     */
    private String proviceName = null;

    /**
     * 手机所在的城市。
     */
    private String cityName = null;

    /**
     * 手机所在的获取区县信息。
     */
    private String village = null;

    /**
     * 街道
     */
    private String street = null;// 街道

    /**
     * 维度。
     */
    private double latitude = 0;

    /**
     * 经度。
     */
    private double Longitude = 0;

    private LocationClient mLocationClient = null;

    private MyLocationListenner myListener = null;


    public LocationClient getmLocationClient() {
        return mLocationClient;
    }

    public String getProviceName() {
        return proviceName;
    }

    public void setProviceName(String proviceName) {
        this.proviceName = proviceName;
    }

    public String getCityName() {
        return cityName;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }

    public String getVillage() {
        return village;
    }

    public void setVillage(String village) {
        this.village = village;
    }

    public double getLatitude() {
        return latitude;
    }

    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }

    public double getLongitude() {
        return Longitude;
    }

    public void setLongitude(double longitude) {
        Longitude = longitude;
    }

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    public String getWantDeleteCityUnil() {
        return wantDeleteCityUnil;
    }

    public String getCurrentUserClickProvice() {
        return currentUserClickProvice;
    }

    public void setCurrentUserClickProvice(String currentUserClickProvice) {
        this.currentUserClickProvice = currentUserClickProvice;
    }

    /**
     * 获取当前用户操作的习惯
     * 
     * @return
     */
    public String getCurrentUserClickCity() {
        return currentUserClickCity;
    }

    public void setCurrentUserClickCity(String currentUserClickCity) {
        this.currentUserClickCity = currentUserClickCity;
    }

    public String getBeijing() {
        return beijing;
    }

    public ArrayList<CountrySortBean> getCountrys() {
        return countrys;
    }

    public void setCountrys(ArrayList<CountrySortBean> countrys) {
        this.countrys = countrys;
    }

    public GetNetDataReturnInfoRespond getCountryInfo() {
        return countryInfo;
    }

    public void setCountryInfo(GetNetDataReturnInfoRespond countryInfo) {
        this.countryInfo = countryInfo;
    }





    public CountryCityFtpInfoRankRespond getTopTDCity() {
        return topTDCity;
    }

    public void setTopTDCity(CountryCityFtpInfoRankRespond topTDCity) {
        this.topTDCity = topTDCity;
    }

    public CountryCityFtpInfoRankRespond getTop2GCity() {
        return top2GCity;
    }

    public void setTop2GCity(CountryCityFtpInfoRankRespond top2gCity) {
        top2GCity = top2gCity;
    }

    public ArrayList<ServersConfigBean> getServiceConfig() {
        return serviceConfig;
    }

    public void setServiceConfig(ArrayList<ServersConfigBean> serviceConfig) {
        this.serviceConfig = serviceConfig;
    }

    public boolean isHaveLogin() {
        return haveLogin;
    }

    public void setHaveLogin(boolean haveLogin) {
        this.haveLogin = haveLogin;
    }


    
    
    
    

}
